package com.sentiment.parser.news;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sentiment.contentextractor.ContentExtractor;
import com.sentiment.contentextractor.News;
import com.sentiment.database.bean.CrawlInfo;
import com.sentiment.tools.Format;
import com.sentiment.webcollector.model.Page;

public class Filter {
	private static final Logger LOG = LoggerFactory.getLogger(Filter.class);

	public static CrawlInfo filterUrl(Page page) {
		List<String> text = null;
		CrawlInfo ci = null;
		if (page.getUrl().matches(WangyiNews.REGEX)) {
			System.out.println("网易");
			WangyiNews wyn = new WangyiNews(page);
			text = wyn.getText();
			ci = new CrawlInfo(new Date(), "", "News", wyn.getTitle(), text, page.getUrl(), wyn.getPublisher(),
					wyn.getReleaseTime(), 0, wyn.getPageView(), wyn.getComment(),
					wyn.getSentiment(list2String(text)).getData1());
			System.out.println(ci);
			return ci;
		} else if (page.getUrl().matches(SohuNews.REGEX)) {
			System.out.println("搜狐");
			SohuNews shn = new SohuNews(page);
			text = shn.getText();
			ci = new CrawlInfo(new Date(), "", "News", shn.getTitle(), shn.getText(), page.getUrl(), shn.getPublisher(),
					shn.getReleaseTime(), 0, shn.getPageView(), shn.getComment(),
					shn.getSentiment(list2String(text)).getData1());
			System.out.println(ci);
			return ci;
		} else if (page.getUrl().matches(SinaNews.REGEX)) {
			System.out.println("新浪");
			SinaNews snn = new SinaNews(page);
			text = snn.getText();
			ci = new CrawlInfo(new Date(), "", "News", snn.getTitle(), snn.getText(), page.getUrl(), snn.getPublisher(),
					snn.getReleaseTime(), 0, snn.getPageView(), snn.getComment(),
					snn.getSentiment(list2String(text)).getData1());
			System.out.println(ci);
			return ci;
		} else if (page.getUrl().matches(IfengNews.REGEX)) {
			System.out.println("凤凰网");
			IfengNews ifn = new IfengNews(page);
			text = ifn.getText();
			ci = new CrawlInfo(new Date(), "", "News", ifn.getTitle(), ifn.getText(), page.getUrl(), ifn.getPublisher(),
					ifn.getReleaseTime(), 0, ifn.getPageView(), ifn.getComment(),
					ifn.getSentiment(list2String(text)).getData1());
			System.out.println(ci);
			return ci;
		} else if (page.getUrl().matches(ChinanewsNews.REGEX)) {
			System.out.println("中国新闻网");
			ChinanewsNews n = new ChinanewsNews(page);
			text = n.getText();
			ci = new CrawlInfo(new Date(), "", "News", n.getTitle(), n.getText(), page.getUrl(), n.getPublisher(),
					n.getReleaseTime(), 0, n.getPageView(), n.getComment(),
					n.getSentiment(list2String(text)).getData1());
			System.out.println(ci);
			return ci;
		} else if (page.getUrl().matches(PeopleNews.REGEX)) {
			System.out.println("人民网");
			PeopleNews n = new PeopleNews(page);
			text = n.getText();
			ci = new CrawlInfo(new Date(), "", "News", n.getTitle(), n.getText(), page.getUrl(), n.getPublisher(),
					n.getReleaseTime(), 0, n.getPageView(), n.getComment(),
					n.getSentiment(list2String(text)).getData1());
			System.out.println(ci);
			return ci;
		} else if (page.getUrl().matches(QqNews.REGEX)) {
			System.out.println("腾讯网");
			QqNews n = new QqNews(page);
			text = n.getText();
			ci = new CrawlInfo(new Date(), "", "News", n.getTitle(), n.getText(), page.getUrl(), n.getPublisher(),
					n.getReleaseTime(), 0, n.getPageView(), n.getComment(),
					n.getSentiment(list2String(text)).getData1());
			System.out.println(ci);
			return ci;
		} else {
			try {
				News n = ContentExtractor.getNewsByHtml(page.getHtml());
				text = new ArrayList<String>();
				text.add(n.getContent());
				ci = new CrawlInfo(new Date(), "", "News", n.getTitle(), text, page.getUrl(), "empty",
						Format.string2Date(n.getTime(),6), 0, 0, 0, 0.0);
				return ci;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return null;
	}

	private static String list2String(List<String> li) {
		StringBuilder sb = new StringBuilder();
		for (String s : li) {
			sb.append(s);
		}
		return sb.toString();
	}



}
